<?php
//пароль
if (!defined('INWEB')) return2();
$a=getVar('a');
includeLang('policies');
define("INPOL", true);
if($a!='' && $a!='add')
{
    try
    {
        $id=getVar('id');
    }
    catch(Exception $e)
    {
        msg($Lang['error'], $e->getMessage(), 'error');
        foot($no_head);
        die;
    }
}
switch($a)
{
    case 'add':
        if($_POST)
            require('module/policies_add_post.php');
        else
            require('module/policies_add.php');
    break;
    case 'view':
        if(!isset($_GET['id'])) break;
        $qry=$sql->query("SELECT * FROM policies WHERE id='$id'");
        if(!$sql->num_rows())
        {
            msg($Lang['error'], $Lang['policy_not_found'],'error');
            break;
        }
        $pol=$sql->fetch_array($qry);
        require('module/policies_view.php');
        break;
    case 'edit':
        if(!isset($_GET['id'])) break;
        $check=$sql->query("SELECT * FROM policies WHERE id='$id'");
        if($sql->num_rows($check))
            $pol=$sql->fetch_array($check);
        else
        {
            msg($Lang['error'],$Lang['policy_not_found'], 'error');
            break;
        }
        if($_POST)
        {
            require('module/policies_edit_post.php');
        }
        else
        {
            require('module/policies_edit.php');
        }
    break;
    case 'del':
    if(!isset($_GET['id'])) break;
    if(isset($_GET['confirm']))
    {
        $sql->query("DELETE FROM `pol_obj` WHERE `pId`='$id';");
        $sql->query("DELETE FROM payments WHERE `id`='$id';");
        $sql->query("DELETE FROM `policies` WHERE `id`='$id';");
        if($sql->row_count)
            msg($Lang['success'], $Lang['pol_success_delete']);
        else
            msg($Lang['warning'], $Lang['pol_failed_delete'], 'warning');
    }
    else
    {
        msg($Lang['warning'],$Lang['are_u_sure_delete_policy'].' <br /> <a href="?p='.$page.'&a=del&id='.$id.'&confirm=1">Yes</a>/<a href="?p='.$page.'">No</a>"', 'warning');
    }
    break;
    default:
        if(isset($_GET['pg']))
	       $start = getVar('pg');
        else 
        	$start = 1;
        if(!is_numeric($start) || $start == 0)
	       $start = 1;
        $start = abs($start) - 1;
        $startlimit = $start * getConfig('settings','items_per_page','100');
        $rows=getConfig('settings','items_per_page','100');
        $selectT=getVar('selectT','string',true);
        $selectV=getVar('selectV','string',true);$npage=$page;
        if($selectT !='' && $selectV!='')
        {
            $npage.='&selectT='.$selectT;
            $npage.='&selectV='.$selectV;
            $qry_add=" WHERE";
            switch($selectT)
            {
                case 'nr':
                    $qry_add.=" `policies`.`nr` LIKE '%$selectV%'";
                break;
                case 'add_nr':
                    $qry_add.=" `policies`.`add_nr` LIKE '%$selectV%'";
                break;
                case 'comp':
                    $qry_add.=" `insurance_companies`.`name` LIKE '%$selectV%' OR `insurance_companies`.`abbr` LIKE '%$selectV%'";
                break;
                case 'ins_type':
                    $qry_add.=" `ins_type`.`name` LIKE '%$selectV%' OR `ins_type`.`abbr` LIKE '%$selectV%'";
                break;
                case 'taker':
                    $qry_add.=" `c1`.`name` LIKE '%$selectV%'";
                break;
                case 'owner':
                    $qry_add.=" `c2`.`name` LIKE '%$selectV%'";
                break;
            }
        }
        $page_foot=$sql->query("SELECT count(*) FROM policies INNER JOIN insurance_companies ON policies.ins_comp_id=insurance_companies.id INNER JOIN clients AS c2 ON policies.owner_id= c2.id INNER JOIN clients AS c1 ON policies.taker_id= c1.id INNER JOIN ins_type ON policies.ins_type_id = ins_type.id LEFT JOIN canceled_policies ON policies.id=canceled_policies.id $qry_add");
        $page_foot = $sql->result($page_foot);
        echo pagechoose($start + 1, $page_foot, $npage);
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a>';
        ?>
    <form method="get" action="index.php">
    <input type="hidden" name="p" value="<?php echo $page;?>" />
    <input type="hidden" name="pg" value="<?php echo $pg;?>" />
    <select name="selectT"><option value=""></option>
    <option value="nr"<?php echo ($selectT=='nr')?' selected="selected"':''; ?>><?php echo $Lang['nr'];?></option>
    <option value="add_nr"<?php echo ($selectT=='add_nr')?' selected="selected"':''; ?>><?php echo $Lang['add_nr'];?></option>
    <option value="comp"<?php echo ($selectT=='comp')?' selected="selected"':''; ?>><?php echo $Lang['ins_comp'];?></option>
    <option value="ins_type"<?php echo ($selectT=='ins_type')?' selected="selected"':''; ?>><?php echo $Lang['ins_type'];?></option>
    <option value="taker"<?php echo ($selectT=='taker')?' selected="selected"':''; ?>><?php echo $Lang['taker'];?></option>
    <option value="owner"<?php echo ($selectT=='owner')?' selected="selected"':''; ?>><?php echo $Lang['owner'];?></option>
    </select> contains <input name="selectV" type="text" value="<?php echo $selectV;?>" /><input type="submit" value="GO" /></form>
    <?php
    $qry=$sql->query("SELECT policies.id, nr, add_nr, insurance_companies.`abbr`, ins_type.`name` as itname, c1.`name` AS tname, c2.`name` AS oname, policies.from_date, policies.to_date, policies.persons_count, policies.ins_prem, (SELECT Sum(payments.sum) AS pay_sum_sub FROM payments WHERE payments.id=policies.id AND (payments.col_date Is Not Null)) as pay_sum, policies.client_attraction, IF(canceled_policies.id IS NULL,'','A') as canceled FROM policies INNER JOIN insurance_companies ON policies.ins_comp_id=insurance_companies.id INNER JOIN clients AS c2 ON policies.owner_id= c2.id INNER JOIN clients AS c1 ON policies.taker_id= c1.id INNER JOIN ins_type ON policies.ins_type_id = ins_type.id LEFT JOIN canceled_policies ON policies.id=canceled_policies.id $qry_add LIMIT $startlimit, $rows");
    echo '<table border="1">';
    echo '<tr><th>'.$Lang['nr'].'</th><th>'.$Lang['add_nr'].'</th><th>'.$Lang['ins_comp'].'</th><th>'.$Lang['ins_type'].'</th><th>'.$Lang['taker'].'</th><th>'.$Lang['owner'].'</th><th>'.$Lang['from_date'].'</th><th>'.$Lang['to_date'].'</th><th>'.$Lang['persons_count'].'</th><th>'.$Lang['ins_prem'].'</th><th>'.$Lang['ins_sum'].'</th><th>'.$Lang['canceled'].'</th><th>'.$Lang['client_attraction'].'</th><th>'.$Lang['actions'].'</th></tr>';
    while($obj=$sql->fetch_array($qry))
    {
        echo '<tr><td>'.$obj['nr'].'</td><td>'.$obj['add_nr'].'</td><td>'.$obj['abbr'].'</td><td>'.$obj['itname'].'</td><td>'.$obj['tname'].'</td><td>'.$obj['oname'].'</td><td>'.$obj['from_date'].'</td><td>'.$obj['to_date'].'</td><td>'.$obj['persons_count'].'</td><td>'.$obj['ins_prem'].'</td><td>'.$obj['pay_sum'].'</td><td>'.$obj['canceled'].'</td><td>'.$obj['client_attraction'].'</td><td><a href="?p='.$page.'&a=view&id='.$obj['id'].'&no_head" target="_blank"><img src="img/view.png" alt="'.$Lang['view'].'" title="'.$Lang['view'].'" /></a><a href="?p='.$page.'&a=edit&id='.$obj['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=del&id='.$obj['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
    }
    echo '</table>';
    echo pagechoose($start + 1, $page_foot, $npage);
    break;
}
?>